package com.hcy.common;

import com.hcy.common.dto.request.PushMsgRequestDTO;
import com.hcy.common.dto.response.MessageResponseDTO;
import com.hcy.rabbitbasicmodel.enums.Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicmodel.signl.Insert;
import com.hcy.rabbitbasicmodel.signl.Query;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/4/13 17:13
 */
@RestController
@RequestMapping("/common-info")
@Slf4j
public class CommonInfoController {

    @Autowired
    CommonInfoBz commonInfoBz;

    /**
     * @Author huchenying
     * @Description 添加单条消息
     * @Date 2022/4/13 17:14
     **/
    @PostMapping("/pushMessage")
    public ResponseData<Object> pushMessage(@Validated(Insert.class) @RequestBody PushMsgRequestDTO requestDTO){
        log.info("pushMessage start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<Object> responseData = new ResponseData<>();
        try {
            commonInfoBz.insertMessage(requestDTO);
            responseData.setCode(Code.Success);
            return responseData;
        } catch (Exception e) {
            log.error("e -> {}",ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
            responseData.setMsg("添加消息失败！");
            return responseData;
        }
    }

    /**
     * @Author huchenying
     * @Description 添加批量消息
     * @Date 2022/4/13 17:14
     **/
    @PostMapping("/pushMessageList")
    public ResponseData<Object> pushMessageList(@Validated(Insert.class) @RequestBody PushMsgRequestDTO requestDTO){
        log.info("pushMessageList start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        return new ResponseData<>();
    }

    /**
     * @Author huchenying
     * @Description 获取消息
     * @Date 2022/4/13 17:14
     **/
    @PostMapping("/queryMessage")
    public ResponseData<List<MessageResponseDTO>> queryMessage(@Validated(Query.class) @RequestBody PushMsgRequestDTO requestDTO){
        log.info("pushMessage start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<List<MessageResponseDTO>> responseData = new ResponseData<>();
        try {
            List<MessageResponseDTO> responseDTOList = commonInfoBz.queryMessage(requestDTO);
            responseData.setCode(Code.Success);
            responseData.setData(responseDTOList);
            return responseData;
        } catch (Exception e) {
            log.error("e -> {}",ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
            responseData.setMsg("添加消息失败！");
            return responseData;
        }
    }
}
